<?php
namespace Borrow\Controller;
use Common\Controller\AdminbaseController;
use Common\Common\Send\SendJssdk;

class BorrowlistadminController extends AdminbaseController{
	function _initialize() {
		parent::_initialize();
						
	}
	function sendsms(){
		$jssdk = new SendJssdk(); 
		//dump($jssdk);
		$smsreturn1=$jssdk->getAccessToken();
		$smsreturn3=$jssdk->getMasUserId();
		$smsreturn2=$jssdk->sendSms('13419835194','您好！我是移动云MAS新超！');
		dump($smsreturn1);
		dump($smsreturn3);
		dump($smsreturn2);
	}
	function sendemail(){		
		$emailreturn1=sp_send_email('freshzhou1983@sina.com','这是一封自动发送的邮件','来自Thinkcmf的邮件哦！');		
		dump($emailreturn1);		
	}
	function tgzcmx(){
		//$lists = $this->smanage_model->order(array("id"=>"asc"))->select();
		//dump($lists);
		//$this->assign('lists',$lists);
		//dump($_SESSION);
		//dump($_SESSION['user']['user_nicename']);
		$admin_name = $_SESSION['name'];//取当前用户的session名字；
		//dump($admin_name);
		$admin_info = D("Users")->where(array("user_login"=>$admin_name))->find();//根据名字从usernas表拿到当前用户信息；
		$admin_nicename = $admin_info['user_nicename'];	//取到该用户的昵称；
		//dump($admin_nicename);	
		$borrow_model = D("Borrow"); //分页使用由此开始，必须先实例化model，可以不用实例Page类。	
		if($admin_nicename==='admin'){	
			$count = $borrow_model->count();
		}else{
				$count = $borrow_model->where(array("xys"=>$admin_nicename))->count();  // 查询满足要求的总记录数	
			}				
		$Page  = $this->Page($count,15);  // 实例化分页类 传入总记录数和每页显示的记录数(15)
		$show  = $Page->show("Admin");   // 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		if($admin_nicename==='admin'){
			$lists  = $borrow_model->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();//当用户是超级管理员时，显示所有贷款信息			
			$lists_count= $borrow_model->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->count();//	
			//$borrowview_model = M('Borrowview');//用的是视图	
			//$map['zhye']  = array('gt',0);	
			$lists_zerocount= $borrow_model->order('id desc')->count();//贷款总笔数			
			$lists_yesum= $borrow_model->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->sum('zhye');//贷款总金额
		}else{
			$lists  = $borrow_model->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->where(array("xys"=>$admin_nicename))->select();//当是普通管理员时，用昵称与使用单位进行比较，只显示所在机构的贷款信息；
			$lists_count = $borrow_model->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->where(array("xys"=>$admin_nicename))->count();//不重复的总记录数
			$lists_yesum= $borrow_model->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->where(array("xys"=>$admin_nicename))->sum('zhye');//
			}			
		$sbjg = M('Sbjg');
		$gjg = $sbjg->select();			
		$this -> assign('gjg',$gjg);
		$this -> assign('lists_zerocount',$lists_zerocount); //输出不为0的总记录数，用的是视图
		$this -> assign('lists_yesum',$lists_yesum); //输出贷款余额		
		$this->assign('lists',$lists);  // 赋值数据集
		$this->assign('Page',$show);   // 赋值分页输出				
		$this->display();     // 输出模板		            
	}
	function zcghjl(){
		//$lists = $this->smanage_model->order(array("id"=>"asc"))->select();
		//dump($lists);
		//$this->assign('lists',$lists);
		//dump($_SESSION);
		//dump($_SESSION['user']['user_nicename']);
		$admin_name = $_SESSION['name'];//取当前用户的session名字；
		//dump($admin_name);
		$admin_info = D("Users")->where(array("user_login"=>$admin_name))->find();//根据名字从usernas表拿到当前用户信息；
		$admin_nicename = $admin_info['user_nicename'];	//取到该用户的昵称；
		//dump($admin_nicename);	
		$borrowmx_model = D("Borrowmx"); //分页使用由此开始，必须先实例化model，可以不用实例Page类。	
		if($admin_nicename==='admin'){	
			$count = $borrowmx_model->count();
		}else{
			$count = $borrowmx_model->where(array("xys"=>$admin_nicename))->count();  // 查询满足要求的总记录数	
		}				
		$Page  = $this->Page($count,15);  // 实例化分页类 传入总记录数和每页显示的记录数(15)
		$show  = $Page->show("Admin");   // 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		if($admin_nicename==='admin'){
			$lists  = $borrowmx_model->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();//当用户是超级管理员时，显示所有贷款信息
			//dump($lists);
			$lists_count= $borrowmx_model->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->count();//总记录数
		}else{
			$lists  = $borrowmx_model->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->where(array("xys"=>$admin_nicename))->select();//当是普通管理员时，用昵称与使用单位进行比较，只显示所在机构的贷款信息；
			$lists_count = $borrowmx_model->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->where(array("xys"=>$admin_nicename))->count();//总记录数
			}		
		//dump($lists);			
		$sbtype = M('Sbtype');
		$gtype = $sbtype->select();	
		$sbjg = M('Sbjg');
		$gjg = $sbjg->select();			
		$this -> assign('gjg',$gjg);
		$this -> assign('lists_count',$lists_count); //输出总记录数		
		$this -> assign('gtype',$gtype);	
		$this -> assign('lists',$lists);  // 赋值数据集
		$this -> assign('Page',$show);   // 赋值分页输出				
		$this -> display();     // 输出模板		            
	}
	function jgsearch(){
		$jg = I("get.jg");     //实现搜索分页的关键1，使用get方式提交
		$borrow_model = D("Borrow");		
		if($jg){
			$condition['xys'] = $jg;    //实现搜索分页的关键2，使用condition
			$jgsearchcount = $borrow_model->where($condition)->count(); // 查询满足要求的总记录数
			$parameter = 'p='.urlencode($_GET['p']);    //实现搜索分页的关键3，截取分页链接的参数，此处是p；
			$Page  = $this->Page($jgsearchcount,15,$parameter);  // 实例化分页类 传入总记录数和每页显示的记录数(15)，实现搜索分页的关键4，实例化分页类时带入$parameter参数；
			
			$show  = $Page->show("Admin");   // 分页显示输出
			$jgsearchlists = $borrow_model->where($condition)->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();			
			$jgsearchsum = $borrow_model->where($condition)->sum('zhye');	
		}
		$this -> assign('jgsearchlists',$jgsearchlists);
		$this -> assign('jgsearchcount',$jgsearchcount);
		$this -> assign('jgsearchsum',$jgsearchsum);
		$this -> assign('Page',$show);   // 赋值分页输出		
		$this -> display();				
	}
	function lscx(){		
		$this->display();
	}	
	function lscx_post(){
		//$typename = I("post.type");
		
		$keyword = I("post.keyword");		
		$borrow_model = D("Borrow");
		//if($typename){
			//$ssearchlists = $smanage_model->where(array("sblb"=>$typename))->select();		
			//dump($ssearchlists);
			//$ssearchcount = $smanage_model->where(array("sblb"=>$typename))->count();
			//dump($ssearchcount);
		//}		
		if($keyword){
			$ssearchlists  = $borrow_model->where(array('name'=>$keyword))->order('id desc')->select();//当用户是超级管理员时，显示所有贷款信息
			$ssearchcount= $borrow_model->where(array('zhdh'=>$keyword))->order('id desc')->count();//不重复的总记录数					
		}		
		$this -> assign('ssearchlists',$ssearchlists);
		$this -> assign('ssearchcount',$ssearchcount);		
		$this -> display();		
	}
	function add(){		
		//$admin_name = $_SESSION['name'];//取当前用户的session名字；
		//dump($admin_name);
		//$admin_info = D("Users")->where(array("user_login"=>$admin_name))->find();//根据名字从usernas表拿到当前用户信息；
		//dump($admin_info);
		//$this -> assign('admin_info',$admin_info);		 
		//$sbtype = M('Cashtype');
		//$gtype = $sbtype->select();
		//$sbjg = M('Sbjg');
		//$gjg = $sbjg->select();				
		//$this -> assign('gtype',$gtype);
		//$this -> assign('gjg',$gjg);			
		$this->display();						
	}
	function checkzhdh(){		
		$zhdh=I("post.zhdh");
		$borrow_model=D('Borrow');
		$zhdhinfo=$borrow_model->where(array('zhdh'=>$zhdh))->find();
		$this->assign('zhdhinfo',$zhdhinfo);
		$zhyegroup = $borrow_model->where(array('zhdh'=>$zhdh))->find();//当用户是超级管理员时，显示所有贷款信息		
		$this->assign('zhyegroup',$zhyegroup);		
		$this->display();						
	}

	function add_post(){
			$borrowmx_model = D('Borrowmx');
			$borrow_model = D('Borrow');
			$zhdh = I("post.zhdh");				
			$zhye = I("post.zhye");								
			$data['name']=I("post.name");
			$data['zhdh']=I("post.zhdh");
			$data['ghlx']=I("post.ghlx");
			$data['xys']=I("post.xys");
			$data['borrow_remarks']='还款';
			$data['add_time'] = date("Y-m-d");
			if(I("post.zhye")>0){				
				$data['zhye'] = I("post.zhye");
				}else{
				$this->error("还款失败，金额不能为空");
			}
			if(!$borrowmx_model->create($data)){
				$this -> error($borrowmx_model->geterror());//添加有自动验证规则时，应在此处进行错误提示和判断，不能在最后$result处写，否则报不出来错误！					
			}else{
				$result = $borrowmx_model->add();
				$borrow_model->where(array('zhdh'=>$zhdh))->setDec('zhye',$zhye);     // 账户余额减还款金额;
				if($result!==false){
					$this->success("还款成功！",U('Borrowlistadmin/zcghjl'));						
				}else{
					$this->error("添加失败！");
				}					
			}
			
		}	

		function edit(){
			$id = intval(I("get.id"));				
			$borrowinfo = D("Borrow")->where(array("id"=>$id))->find();					
			$this -> assign('borrowinfo',$borrowinfo);									
			$this -> display();								
		}
		
		function edit_post(){
			$id = intval(I("post.id"));
			$borrow_money = I('post.borrow_money');
			$borrow_interest = I('post.borrow_interest');
			$b_money = I('post.b_money');
			$b_interest = I('post.b_interest');					
			$borrow_model = D('Borrow');
			$pay_money=I('post.b_money') - I('post.borrow_money');
			$pay_interest=I('post.b_interest') - I('post.borrow_interest');			
			$data = array('borrow_money'=>$pay_money,'borrow_interest'=>$pay_interest,'add_time'=>date("Y-m-d H:i:s"));			
			if(!$borrow_model->create($data)){
				$this->error($borrow_model->geterror());//添加有自动填充规则时，应在此处进行错误提示和判断，不能在最后$result处写，否则报不出来错误！
			}else{
				$result = $borrow_model->where(array("id"=>$id))->setField($data);
			}			
			//dump($result);
			if($result!==false){
				$this->success("修改成功！",U('Borrowlistadmin/index'));
			}else{
				$this->error("修改失败！");
			}
		}
		
		function delete(){
			if(isset($_GET['id'])){
				$id = intval(I('get.id'));
				$result = D("Borrow")->where(array("id"=>$id))->delete();
				if($result!==false){
					$this->success("删除成功！", U("Borrowlistadmin/zcghjl"));
					}else{
					$this->error('删除失败！');
				}
			}
			if(isset($_POST['ids'])){
				$ids=join(",",$_POST['ids']);
				$result = D("Smanage")->where("id in ($ids)")->delete();			
				if ($result!==false) {
					$this->success("删除成功！",U("Borrowlistadmin/tgzcmx"));
				} else {
					$this->error("删除失败！");
				}
			}			
		}
		function showinfo(){
			$id = intval(I('get.id'));
			$borrow_model = D('Borrow');
			$showinfo = $borrow_model->where(array("id"=>$id))->find();
			$this -> assign('showinfo',$showinfo);			
			$this->display();
			//dump($showinfo['id']);			
		}

		function printinfo(){
			$id = intval(I('get.id'));
			$borrowmx_model = M('Borrowmx');		
			$ghinfo = $borrowmx_model->where(array("id"=>$id))->find();
			$zhdh = $ghinfo['zhdh'];
			$borrow_model = D('Borrow');
			$dkinfo = $borrow_model->where(array('zhdh'=>$zhdh))->find();			
			$this -> assign('ghinfo',$ghinfo);
			$this -> assign('dkinfo',$dkinfo);				
			$this->display();
			//dump($showinfo['id']);			
		}
		function zchsbb(){				
			$this->display();			
		}
		
		function tjbb_post(){
			$begindate = I("post.begindate");
			$this->assign('begindate',$begindate);	
			$overdate = I("post.overdate");	
			//dump($overdate);			
			$borrow_model = D("Borrow"); 
			$borrowmx_model = D("Borrowmx");			
			$qclists  = $borrow_model->field('xys,sum(zhye) as qcye,count(zhdh) as qcbs')->where(array("borrow_remarks"=>''))->group('xys')->order('xys desc')->select();
			$condition['add_time']=array('between',array($begindate,$overdate));
			$dqlists  = $borrowmx_model->field('xys as xys,sum(zhye) as dqye,count(zhdh) as dqbs,sum(ghlx) as dqlx')->where($condition)->group('xys')->order('xys desc')->select();
			//$lists = array_merge($qclists,$dqlists);	
			$ljlists  = $borrowmx_model->field('xys,sum(zhye) as ljye,count(zhdh) as ljbs')->group('xys')->order('xys desc')->select();			
			$map['zhye'] = array('neq',0);		
			$qmlists  = $borrow_model->field('xys,count(zhye) as qmbs,sum(zhye) as qmye')->where($map)->group('xys')->order('xys desc')->select();
			$this->assign('qclists',$qclists);	
			$this->assign('dqlists',$dqlists);
			$this->assign('ljlists',$ljlists);
			$this->assign('qmlists',$qmlists);	
			$this->display();     // 输出模板		            
		}		
}